package com.ufcg.lvc.client.prototipacao;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FileUpload;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.ufcg.lvc.client.GreetingServiceCRUD;
import com.ufcg.lvc.client.GreetingServiceCRUDAsync;
import com.ufcg.lvc.client.UserData;

public class EditarInstituicao {

	AbsolutePanel panel;
	UserData UD;
	public EditarInstituicao(final String nomeAntigo, final String cabecalhoAntigo, UserData uD) {
		UD = uD;
		final AsyncCallback<Boolean> callback = new AsyncCallback<Boolean>() {
			@Override
			public void onFailure(Throwable caught) {
				System.out.println("Erro na comunicação com o servidor");
			}

			@Override
			public void onSuccess(Boolean result) {
			}
		};
		
		panel = new AbsolutePanel();
		panel.setSize("900px", "707px");
		
		Label tituloLB = new Label("Editar instituição");
		tituloLB.setStyleName("gwt-TituloMedio");
		panel.add(tituloLB, 0, 10);
	
		final TextBox nomeTB = new TextBox();
		panel.add(nomeTB, 143, 69);
		nomeTB.setText(nomeAntigo);
		nomeTB.setSize("457px", "17px");
				
		Label lblNome = new Label("Nome:");
		panel.add(lblNome, 60, 69);
		
		Label lblCabecalho = new Label("Cabeçalho:");
		panel.add(lblCabecalho, 60, 104);
		lblCabecalho.setSize("47px", "17px");
		
		final TextArea cabecalhoTA = new TextArea();
		cabecalhoTA.setText(cabecalhoAntigo);
		panel.add(cabecalhoTA, 143, 104);
		cabecalhoTA.setSize("457px", "100px");
		
		Label lblLogo = new Label("Logo:");
		panel.add(lblLogo, 60, 223);
		lblLogo.setSize("47px", "17px");
		
		final FileUpload logoFU = new FileUpload();
		panel.add(logoFU, 143, 226);
		logoFU.setSize("319px", "26px");

		com.google.gwt.user.client.ui.Button btnEditarAluno = new com.google.gwt.user.client.ui.Button("Editar");
		panel.add(btnEditarAluno, 243, 	303);
		btnEditarAluno.setSize("121px", "29px");
		
		Button btnVoltar = new com.google.gwt.user.client.ui.Button("Voltar");
		panel.add(btnVoltar, 367, 303);
		btnVoltar.setSize("121px", "29px");
        
		btnEditarAluno.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				getServicoCRUD().updateInstituicao(nomeAntigo, nomeTB.getText(), cabecalhoTA.getText(), UD.getId_usuario(), callback);
				voltar();
			}
		});
		
		btnVoltar.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				voltar();
			}
		});
	}
	
	private void voltar(){
		panel.clear();
		MinhasInstituicoes m = new MinhasInstituicoes(UD);
		panel.add(m.getPanel());
	}
	public static GreetingServiceCRUDAsync getServicoCRUD() {
		return GWT.create(GreetingServiceCRUD.class);
	}

	public AbsolutePanel getPanel() {
		return panel;
	}
}
